﻿using System;
using System.Drawing;
using System.Reflection;
using System.Windows;
using System.Windows.Forms;

namespace AutoLoL.Logic.Utils
{
    public class MinimizeToTray
    {
        // TODO
        // NOTE - The code has to be refactored to work with the new architecture

        /*private MainWindow _window;
        private NotifyIcon _notifyIcon;

        public MinimizeToTray(MainWindow window, bool isEnabled = true)
        {
            Version version = Assembly.GetExecutingAssembly().GetName().Version;
            string versionString = "AutoLoL v" + version.Major + "." + version.Minor + "." + version.Build;

            ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
            contextMenuStrip.Items.Add(versionString);
            contextMenuStrip.Items.Add("-");
            contextMenuStrip.Items.Add("Launch League of Legends", null, ContextLaunchLol_MouseClick);
            contextMenuStrip.Items.Add("Launch Updater", null, ContextUpdater_MouseClick);
            contextMenuStrip.Items.Add("-");
            contextMenuStrip.Items.Add("Mastery Editor Tab", null, ContextEditor_MouseClick);
            contextMenuStrip.Items.Add("Mastery Browser Tab", null, ContextBrowser_MouseClick);
            contextMenuStrip.Items.Add("-");
            contextMenuStrip.Items.Add("Exit", null, ContextClose_MouseClick);
            contextMenuStrip.Items[0].Enabled = false;

            this._notifyIcon = new NotifyIcon();
            this._notifyIcon.Text = versionString;
            this._notifyIcon.ContextMenuStrip = contextMenuStrip;
            this._notifyIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location);
            this._notifyIcon.MouseClick += new MouseEventHandler(NotifyIcon_MouseClick);
            this._notifyIcon.BalloonTipClicked += new EventHandler(ContextBrowser_MouseClick);

            this._window = window;

            this.IsEnabled = isEnabled;
        }

        public bool IsEnabled
        {
            set
            {
                if (value)
                {
                    _window.StateChanged += new EventHandler(Window_StateChanged);
                }
                else
                {
                    _window.StateChanged -= Window_StateChanged;
                    _window.ShowInTaskbar = true;
                }
            }
        }

        /// <summary>
        /// Handles the Window's StateChanged event.
        /// </summary>
        /// <param name="sender">Event source.</param>
        /// <param name="e">Event arguments.</param>
        private void Window_StateChanged(object sender, EventArgs e)
        {
            // Show/hide Window and NotifyIcon
            bool isMinimized = (_window.WindowState == WindowState.Minimized);
            _window.ShowInTaskbar = !isMinimized;
            _notifyIcon.Visible = isMinimized;
        }

        /// <summary>
        /// Handles a click on the notify icon or its balloon.
        /// </summary>
        /// <param name="sender">Event source.</param>
        /// <param name="e">Event arguments.</param>
        private void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            // Restore the Window on left mouse click
            // Show context menu on right mouse click
            if (e.Button == MouseButtons.Left)
            {
                _window.WindowState = WindowState.Normal;
            }
            else if (e.Button == MouseButtons.Right)
            {
                _notifyIcon.ContextMenuStrip.Show(Control.MousePosition);
            }
        }

        /// <summary>
        /// Handles a click on the mastery editor option in the contextmenu.
        /// Restores the main window, after selecting the mastery editor tab.
        /// </summary>
        /// <param name="sender">Event source.</param>
        /// <param name="e">Event arguments.</param>
        private void ContextEditor_MouseClick(object sender, EventArgs e)
        {
            _window.RootTabControl.SelectedIndex = MainWindow.MASTERIES_EDITOR_TAB_INDEX;
            _window.WindowState = WindowState.Normal;
        }

        /// <summary>
        /// Handles a click on the mastery browser option in the contextmenu.
        /// Restores the main window, after selecting the mastery browser tab.
        /// </summary>
        /// <param name="sender">Event source.</param>
        /// <param name="e">Event arguments.</param>
        private void ContextBrowser_MouseClick(object sender, EventArgs e)
        {
            _window.RootTabControl.SelectedIndex = MainWindow.MASTERIES_BROWSER_TAB_INDEX;
            _window.WindowState = WindowState.Normal;
        }

        /// <summary>
        /// Handles a click on the close option in the contextmenu.
        /// </summary>
        /// <param name="sender">Event source.</param>
        /// <param name="e">Event arguments.</param>
        private void ContextClose_MouseClick(object sender, EventArgs e)
        {
            _window.Close();
        }

        /// <summary>
        /// Handles a click on the launch lol option in the contextmenu.
        /// </summary>
        /// <param name="sender">Event source.</param>
        /// <param name="e">Event arguments.</param>
        private void ContextLaunchLol_MouseClick(object sender, EventArgs e)
        {
            _window.LaunchLeagueOfLegends();
        }

        /// <summary>
        /// Handles a click on the upate option in the contextmenu.
        /// </summary>
        /// <param name="sender">Event source.</param>
        /// <param name="e">Event arguments.</param>
        private void ContextUpdater_MouseClick(object sender, EventArgs e)
        {
             _window.WindowState = WindowState.Normal;
            _window.UpdaterButton_Click(this, null);
         }*/
    }
}